#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

class Solution {
    public:
	vector<int> minInterval(vector<vector<int>> &intervals, vector<int> &queries)
	{
		sort(intervals.begin(), intervals.end());

		vector<pair<int, int>> req;
		for (int i = 0, len, expire; i < queries.size(); i++) {
			req.push_back({ queries[i], i });
		}
		sort(req.begin(), req.end());

		vector<int> ans(queries.size(), 0);
		priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
		int id = 0;
		for (int i = 0; i < req.size(); i++) {
			while (id < intervals.size() && intervals[id][0] <= req[i].first) {
				pq.push({ intervals[id][1] - intervals[id][0] + 1, intervals[id][1] });
				id++;
			}

			while (!pq.empty() && pq.top().second < req[i].first) {
				pq.pop();
			}

                        ans[req[i].second] = pq.empty() ? -1 : pq.top().first;
		}
		return ans;
	}
};

int main()
{
	vector<vector<int>> intervals = { { 1, 4 }, { 2, 4 }, { 3, 6 }, { 4, 4 } };
	vector<int> queries = { 2, 3, 4, 5 };
	auto ans = Solution().minInterval(intervals, queries);
	for (auto v : ans) {
		cout << v << " ";
	}
	cout << endl;
	return 0;
}